package thread;

import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class MyThread  extends Thread {
    @Override
    public void run() {
        System.out.println("I'm thread code");
    }
    public static void main(String[] args) {
        new MyThread().start();
    }
}

class MyThreadTest1 {
    private static volatile Integer i = 0;
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int j = 0; j < 100 ; j++) {
                    i++;
                    synchronized (""){
                    }
                    try {
                        TimeUnit.MILLISECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int j = 0; j < 100 ; j++) {
                    i++;
//                    synchronized (""){
//                    }
                    try {
                        TimeUnit.MILLISECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(i);
    }
}

class MyThreadTestVolatile {
    // 使用volatile 修饰 并发访问的变量，可以解决多线程 可见性 问题（无法保证原子性）
    private static volatile Boolean flag = true;
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                flag = false;
            }
        }).start();

        while (flag) {

        }
        System.out.println("main over");
    }
}
class Test3 {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized ("a") {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized ("b") {
                        //
                        System.out.println("t1 over");
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized ("b") {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized ("a") {
                        //
                        System.out.println("t2 over");
                    }
                }
            }
        }).start();
        System.out.println("main over");
    }
}
